#!/usr/bin/env python3
# -*- encoding: utf-8 -*-
'''
@File    :   utils.py
@Time    :   2023/04/07 22:06:00
@Author  :   youle
@Email   :   a_fly0505@163.com
@Copyright : 侵权必究
'''

import datetime
import logging
import os
from logging import handlers, Logger

logger: Logger = None

def LogConfig():
    # logging.basicConfig(format='%(asctime)s - %(pathname)s[line:%(lineno)d] '
    #                     '- %(levelname)s: %(message)s', level=logging.INFO)
    if not os.path.exists("log"):
        os.mkdir("log")
    global logger
    logger = logging.getLogger('stock')
    logger.setLevel(level=logging.DEBUG)
    # logger.propagate = False
 
    formatter = logging.Formatter('%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s')

    stream_handler = logging.StreamHandler()
    stream_handler.setLevel(logging.DEBUG)
    stream_handler.setFormatter(formatter)
    logger.addHandler(stream_handler)
 
    # 实例化TimedRotatingFileHandler
    # interval是时间间隔
    # backupCount是备份文件的个数，如果超过这个个数，就会自动删除
    # when是间隔的时间单位，单位有以下几种：S 秒；M 分；H 小时；D 天；W 每星期（interval==0时代表星期一）；midnight 每天凌晨
    # handlers.RotatingFileHandler(filename='stock_log.log', mode='a', maxBytes=60*1024*1024, backupCount=5, encoding=None, delay=False)
    time_rotating_handler = handlers.TimedRotatingFileHandler(
                                      filename='log/stockd.log', 
                                      when='D', 
                                      backupCount=10,
                                      encoding='utf-8')  # 往文件里写入#指定间隔时间自动生成文件的处理器
    time_rotating_handler.setFormatter(formatter)  # 设置文件里写入的格式
    time_rotating_handler.setLevel(level=logging.INFO)
    logger.addHandler(time_rotating_handler)

def IsOneDay(today: datetime.date, lastDate: datetime.date) -> bool:
    if today.isoweekday() == 1 and (today - lastDate).days == 3:
        return True
    elif (today - lastDate).days <= 1:
        return True
    else:
        return False
